<?php

namespace app\admin\controller;


use app\admin\validate\AdminUser;

class  Login extends AdminBase
{
    //登录页面初始化 加判断
    public function initialize()
    {
        if ($this->isLogin()) {
            return $this->redirect(url("index/index"));
        }
    }

    public function index()
    {

        return view();
    }

    public function add()
    {
        halt(session(Config("admin.session_admin")));

    }


    public function check()
    {
        if (!$this->request->isPost()) {
            return show(config("status.error"), "请求方式错误");
        }

        //过滤  获取数据
        $username = $this->request->param("username", "", "trim");
        $password = $this->request->param("password", "", "trim");
        $captcha = $this->request->param("captcha", "", "trim");

        $data = [
            'username' => $username,
            'password' => $password,
            'captcha' => $captcha,
        ];

        //validate验证数据
        $validate = new AdminUser();
       //halt($validate);
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        //登录逻辑
        try {
            $result = \app\admin\business\AdminUser::login($data);
        } catch (\Exception $e) {
            return show(config("status.error"), $e->getMessage());
        }

        return show(config("status.success"), "登录成功");

        // return show(config("status.success"), "登录成功");
    }

}
